﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MsgSender.models;
using MsgSender.buz;

namespace MsgSender
{
    public partial class FrmMain : Form
    {
        public Timer timerSearch = null;

        public Queue<CWindow.CWindow> queWin = new Queue<CWindow.CWindow>();

        //当前处理处理窗口
        public CWindow.CWindow winCurrent = null;
        //当前窗口处理顺序
        public int currentHandelIndex = 0;

        public Timer timerHandel = null;



        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //窗口搜寻
            timerSearch = new Timer();
            timerSearch.Interval = AppSetting.AppSettings.intrSearchTime * 1000;
            timerSearch.Tick += new System.EventHandler(this.timerSearch_Tick);

            //窗口处理
            timerHandel = new Timer();
            timerHandel.Interval = 80;
            timerHandel.Tick += new System.EventHandler(this.timerHandel_Tick);

            btnEnd.Enabled = false;

        }

        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnEnd.Enabled = true;
            timerSearch.Start();

        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnd_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = true;
            btnEnd.Enabled = false;
            timerSearch.Stop();
        }


        /// <summary>
        /// 窗口搜寻timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerSearch_Tick(object sender, EventArgs e)
        {
            AppManager.appManager.GetMatchWindows();
            //清空队列
            queWin.Clear();
            foreach (CWindow.CWindow cw in AppManager.appManager.windows)
                queWin.Enqueue(cw);
                //启动定时器
            timerHandel.Start();
            currentHandelIndex = 0;
          }
        private void timerHandel_Tick(object sender, EventArgs e)
        {
            switch (currentHandelIndex)
            {
                 //开始
                case 0:
                    if (queWin.Count <= 0)
                    {
                        timerHandel.Stop();
                        currentHandelIndex = 0;
                        return;
                    }
                    winCurrent = queWin.Dequeue();
                    //需发送信息
                    if (SendMsgBuz.SendMessage(winCurrent))
                    {
                        winCurrent.ShowWindow();
                        currentHandelIndex++;
                    }
                    else
                        currentHandelIndex = 3;
                    break;
                case 1:
                    //模拟按键
                    winCurrent.ShowWindow();
                    winCurrent.AnalogPaste();
                    currentHandelIndex++;
                    break;
                case 2:
                    winCurrent.ShowWindow();
                    //发送键
                    if (AppSetting.AppSettings.EMSendHotKey.CtrlEnter == AppSetting.AppSettings.sendHotKey)
                    {
                        win32API.win32API.keybd_event((byte)Keys.ControlKey, 0, 0, 0);//CTRL 按下
                        win32API.win32API.keybd_event((byte)Keys.Enter, 0, 0, 0);//ENTER 按下
                        win32API.win32API.keybd_event((byte)Keys.ControlKey, 0, 2, 0);//CTRL 释放
                        win32API.win32API.keybd_event((byte)Keys.Enter, 0, 2, 0);//ENTER 释放
                    }
                    else {
                        win32API.win32API.keybd_event((byte)Keys.Enter, 0, 0, 0);//ENTER 按下
                        win32API.win32API.keybd_event((byte)Keys.ControlKey, 0, 2, 0);//CTRL 释放
                    }
                    currentHandelIndex++;
                    break;
                case 3:
                    winCurrent.ShowWindow();
                    //关闭窗口
                    winCurrent.CloseWindow();
                    currentHandelIndex = 0;
                    break;
                default:
                    currentHandelIndex = 0;
                    break;
            }

            
        

        }


           

        }

      
    }


//需要发送数据
                //if (SendMsgBuz.SendMessage(cw)) {
                 //   cw.ShowWindow();
                   // cw.AnalogPaste();
                    /*
                    //发送键
                    if (AppSetting.AppSettings.EMSendHotKey.CtrlEnter == AppSetting.AppSettings.sendHotKey)
                    {
                        win32API.win32API.keybd_event((byte)Keys.ControlKey, 0, 0, 0);//CTRL 按下
                        win32API.win32API.keybd_event((byte)Keys.Enter, 0, 0, 0);//ENTER 按下
                        win32API.win32API.keybd_event((byte)Keys.ControlKey, 0, 2, 0);//CTRL 释放
                        win32API.win32API.keybd_event((byte)Keys.Enter, 0, 2, 0);//ENTER 释放
                    }
                    else {
                        win32API.win32API.keybd_event((byte)Keys.Enter, 0, 0, 0);//ENTER 按下
                        win32API.win32API.keybd_event((byte)Keys.ControlKey, 0, 2, 0);//CTRL 释放
                    }*/

                //}
              //  cw.CloseWindow();
